package com.jinan.config;

import com.jinan.annotation.MetricTime;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LogAspect {

    /**
     * 这是注释
     * @param joinPoint
     * @param metricTime
     * @return
     * @throws Throwable
     */
    @Around("@annotation(metricTime)")
    public Object log(ProceedingJoinPoint joinPoint, MetricTime metricTime) throws Throwable {
        String name = metricTime.value();
        long start = System.currentTimeMillis();
        try {
            return joinPoint.proceed();
        } finally {
            long t = System.currentTimeMillis() - start;
            System.err.println("[Metrics] " + name + ": " + t + "ms");
        }
    }
}
